package com.fanbai.score.business.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.business.beans.OrderInfo_Bean;
import com.fanbai.score.business.http.Busin_Order_RequestNet;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by acer on 2017/6/18.
 */

public class OrderList_Activity extends BaseActivity{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.businessActivityOrderList_cnt)
    TextView cntTxt;
    @BindView(R.id.businessActivityOrderList_totlePrice)
    TextView totlePriceTxt;

    @Override
    protected int getLayoutID() {
        return R.layout.business_activity_order;
    }

    @Override
    public String getTitleName() {
        return "订单列表";
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        String smuid = getIntent().getExtras().getString("smuid");
        getorderinfosbysmuid(smuid);
        getorderstatfortoday(smuid);
    }

    private void setData(final List<OrderInfo_Bean> beans){
//        List<String> dataList = new ArrayList<>();
//        dataList.add("htsec01.png");
//        dataList.add("htsec01.png");
//        dataList.add("htsec01.png");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        CommonAdapter adapter = new CommonAdapter<OrderInfo_Bean>(ctx, R.layout.business_item_activity_orderlist,
                beans){
            @Override
            protected void convert(final ViewHolder holder, final OrderInfo_Bean bean, int position) {
                holder.setText(R.id.businessItemActivityOrderList_orderId, "订单："+bean.getOrderid());
                holder.setText(R.id.businessItemActivityOrderList_orderTime, ""+bean.getOcreatetime());
                holder.setText(R.id.businessItemActivityOrderList_orderPrice, "总价："+bean.getOtotalprice());
                holder.setText(R.id.businessItemActivityOrderList_orderScore, "积分："+bean.getOscore());
                holder.setText(R.id.businessItemActivityOrderList_orderDec, "描述："+bean.getOproductdesc());
                int ostate = bean.getOstate();
                TextView stateText = holder.getView(R.id.businessItemActivityOrderList_orderState);
                TextView butText = holder.getView(R.id.businessItemActivityOrderList_orderBut);
                stateText.setTextColor(getResources().getColor(R.color.color_font_black_white));
                butText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        remindorder(bean.getOid());
                    }
                });
                if(ostate == 0){
                    stateText.setTextColor(getResources().getColor(R.color.color_font_red));
                    stateText.setText("等待扫码");
                    butText.setVisibility(View.VISIBLE);
                    butText.setText("督促");
                }else if(ostate == 5){
                    if(bean.getOproductdesc() == null || bean.getOproductdesc().equals("")){
                        stateText.setText("备注未填写");
                        butText.setVisibility(View.VISIBLE);
                        butText.setText("督促");
                    }else{
                        stateText.setText("已完成");
                        butText.setVisibility(View.GONE);
                    }
                }else{
                    stateText.setText("已完成");
                    butText.setVisibility(View.GONE);
                }



            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("oid", beans.get(position).getOid());
                jumpToActivity(OrderDetails_Activity.class, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishActivity();
    }

    private void getorderinfosbysmuid(String smuid){
        showDialog();
        Busin_Order_RequestNet.getorderinfosbysmuid(ctx, smuid, new CallBack<List<OrderInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<OrderInfo_Bean> beans) {
                hideDialog();
                setData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });

    }
    private void remindorder(String oid){
        showDialog();
        Busin_Order_RequestNet.remindorder(ctx, oid, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                hideDialog();
                initData();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void getorderstatfortoday(String smuid){

        Busin_Order_RequestNet.getorderstatfortoday(ctx, smuid, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                cntTxt.setText("今日服务"+JsonUtils.getString(s, "custcount")+"个用户");
                totlePriceTxt.setText("总金额"+JsonUtils.getString(s, "totalprice")+"元");
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
